package com.code.generator.cn.util.base;

public class MyStringChangeUtil {
	
	
	
	/**
	 * 将对象名转换成数据库表名
	 * @param name
	 * @return
	 */
	public static String changeDomain2Table(String name){
		StringBuilder tableName=new StringBuilder("");
		for (int i = 0; i < name.length(); i++) {
			char c=name.charAt(i);
			//如果对象名开头是首写字母大写直接改换成小写
			if(Character.isUpperCase(c)){
				//如果首字母开头是大写那改成小写
				if(i==0){
					tableName.append(Character.toLowerCase(c));
				//如果不是首字母大写 那直接转换成_小写字母
				}else{
					tableName.append("_"+Character.toLowerCase(c));
				}
			}else{
				tableName.append(c);
			}
		}
		return  tableName.toString();
	}
	
	
	
	/**
	 * 转换的字段类型
	 * @param colType 要转换的字段
	 * @return
	 */

	public static String changeFileld(String colType) {
		if(colType.equals("INT")){
			 colType="Integer";
		 }else if(colType.equals("VARCHAR")){
			 colType="String";
		 }else if(colType.equals("DATETIME")){
			 colType="Date";
		 }else if(colType.equals("LONG")||colType.equals("BIGINT UNSIGNED")){
			 colType="Long";
		 }else if(colType.equals("DOUBLE")){
			 colType="Double";
		 }else if(colType.equals("FLOAT")){
			 colType="Float";
		 }else if(colType.equals("BOOLEAN")){
			 colType="Boolean";
		 }else if(colType.equals("CHAR")){
			 colType="String";
		 }else if(colType.equals("BIT")){
			 colType="byte";
		 }else if(colType.equals("DATE")){
			 colType="Date";
		 }else if(colType.equals("BIGINT")){
			 colType="Long";
		 }else if(colType.equals("DECIMAL")){
			 colType="Float";
		 }else if(colType.equals("TINYINT")){
			 colType="Byte";
		 }else if(colType.equals("TEXT")){
			 colType="String";
		 }
		return colType;
	}
	
	
	
	/**
	 * 
	 * @param name 要转换的字段
	 * @param changeType 要转换的类型 1.表名 2.字段名,3方法名
	 * @return
	 */
	

	public  static String changeField(String name,int changeType) {
		String[] classNames=name.split("_");
		 String className="";
		className = getString(changeType, classNames, className);
		return className;
	}

	public static String getString(int changeType, String[] classNames, String className) {
		for (int i = 0; i < classNames.length; i++) {
			if(changeType==1){
					className+=classNames[i].substring(0,1).toUpperCase()+classNames[i].substring(1);
			}else if(changeType==2){
				if(i==0){
					className+=classNames[i];
				}else{
					className+=classNames[i].substring(0,1).toUpperCase()+classNames[i].substring(1);
				}
			}else if(changeType==3){
				  className+=classNames[i].substring(0,1).toUpperCase()+classNames[i].substring(1);
			}
	   }
		return className;
	}

	/**
	 * 
	 * @param str 要转换的 对象字段名称
	 * @return 返回数据库字段名称如  dic_name
	 */
	
	public static  String  changeFileName(String str){
		StringBuilder newFileName=new StringBuilder();
		for (int i = 0; i < str.length(); i++) {
			char c=str.charAt(i);
			if(Character.isUpperCase(c)){
				newFileName.append("_"+Character.toLowerCase(c));
			}else{
				newFileName.append(c);
				
			}
		}
		return newFileName.toString();
	}

	public static Object changeFiledType(String fieldType) {
		if(fieldType.equals("INT")){
			return "INTEGER";
		}
		if(fieldType.equals("DATETIME")){
			return "TIMESTAMP";
		}
		if(fieldType.equals("TEXT")){
			return "VARCHAR";
		}

		return fieldType;
	}
}
